<?php

/**
 * UsuariosModel
 *  
 * @author diego
 * @version 
 */

require_once 'Zend/Db/Table/Abstract.php';

class Usuarios extends Zend_Db_Table_Abstract {

	protected $_name = 'usuarios';

	protected $_dependentTables  = array('Textos', 'Respostas');
	
	
	
	
	
	/**
	 * Faz uma busca de usuarios baseada numa palavra-chave
	 * 
	 * @param 
	 * @return Usuarios
	 */
	public function search($busca, $usuario = '', $ordem = '') {
		$users = array ( );
		
		$select = $this->getAdapter ()->select ()->from ( array ('user' => 'usuarios') )->where ( "user.nome LIKE ?", "%$busca%" );
		
		$resultados = $this->getAdapter ()->fetchAll ( $select );
		
		foreach ( $resultados as $r ) {
			
			$condicao = $this->getAdapter ()->quoteInto ( "id = ?", $r ['id'] );
			
			if ($usuario)
				$condicao .= " and " . $this->getAdapter ()->quoteInto ( "id_usuario = ?", $usuario );
			
			$item = $this->fetchAll ( $condicao, $ordem )->current ();
			$users [] = $item;
		
		}
		return $users;
	}
	
	public function searchByEmail($busca, $usuario = '', $ordem = '') {
		
		
		$select = $this->getAdapter ()->select ()->from ( array ('user' => 'usuarios') )->where ( "user.email LIKE ?", "$busca" );
		
		$resultado = $this->getAdapter ()->fetchAll ( $select );
		
		foreach ( $resultado as $r ) {
			
			$condicao = $this->getAdapter ()->quoteInto ( "id = ?", $r ['id'] );
			
			if ($usuario)
				$condicao .= " and " . $this->getAdapter ()->quoteInto ( "id_usuario = ?", $usuario );
				
			$item = $this->fetchAll ( $condicao, $ordem )->current ();
			$u = $item;
		
		}
		return $u;
		
		
	}
	
	
	
	
	
	/**
	 * Pega todos os usuarios
	 * 
	 * @param string ordem
	 * @return array
	 */
	public function findAll($ordem = '') {
		return $this->fetchAll ( null, $ordem );
	}
	
	/**
	 * Atualiza o usuario no banco
	 * 
	 * @param array dados, string where
	 */
	public function update(array $dados, $where) {
		
		parent::update ( $dados, $where );
	
	}
	
	public function delete($id) {
		parent::delete ( $this->getAdapter ()->quoteInto ( "id = ?", $id ) );
	}
	  
}
